//Source file: D:\\test\\com\\ce\\pms\\custservice\\service\\impl\\TrackRecordServiceImpl.java

package com.ce.pms.custservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.custservice.dao.ICustServiceEventDao;
import com.ce.pms.custservice.dao.ITrackRecordDao;
import com.ce.pms.custservice.model.CustServiceEventDO;
import com.ce.pms.custservice.model.TrackRecordDO;
import com.ce.pms.custservice.service.ITrackRecordService;
import com.ce.pms.custservice.vo.TrackRecordVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.system.custservice.model.ReturnVisitWayDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述: 事件跟踪Service功能类
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 胡煦 Create on:2008-12-24 Edit author: Edit on: 修改原因：
 *
 */

public class TrackRecordServiceImpl extends QuestionaireServiceImpl implements
		ITrackRecordService {

	/**
	 * @roseuid 49436CB30167
	 */
	public TrackRecordServiceImpl() {

	}

	/**
	 * 方法描述: 根据ID查询TrackRecordVO方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param id
	 * @return TrackRecordVO
	 * @roseuid 49436CB30186
	 */
	public TrackRecordVO getTrackRecordVOByID(Long id) {
		this.checkParameterIsNull(id,
				"custservice:custservice_parameter_null_exception");
		return this.assembleD2V(trackRecordDao.findById(id), null);
	}

	/**
	 * 方法描述: 查询TrackRecordVOList方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param csEventID
	 * @return List<TrackRecordVO>
	 * @roseuid 49436CB301A5
	 */
	public List<TrackRecordVO> queryTrackRecordVOList(Long csEventID) {
		this.checkParameterIsNull(csEventID,
				"custservice:custservice_parameter_null_exception");
		List<TrackRecordDO> trackRecordDOList = trackRecordDao
				.queryTrackRecordByCsEventID(csEventID);
		List<TrackRecordVO> trackRecordVOList = null;
		if (trackRecordDOList.size() > 0) {
			trackRecordVOList = new ArrayList<TrackRecordVO>();
			for (TrackRecordDO trackRecordDO : trackRecordDOList) {
				trackRecordVOList.add(this.assembleD2V(trackRecordDO, null));
			}
		}
		return trackRecordVOList;
	}

	/**
	 * 方法描述: 分页查询TrackRecordVO方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param search
	 * @return Page
	 * @roseuid 49436CB301C5
	 */
	public Page queryTrackRecordByPage(Long csEventID, SearchVO search) {
		this.checkParameterIsNull(csEventID,
				"custservice:custservice_parameter_null_exception");
		if (BlankUtil.isBlank(search)) {
			return null;
		}
		Page page = trackRecordDao
				.queryTrackRecordByPage(csEventID, search);
		List<TrackRecordDO> trackRecordDOList = page.getResult();
		List<TrackRecordVO> trackRecordVOList = null;
		if (trackRecordDOList.size() > 0) {
			trackRecordVOList = new ArrayList<TrackRecordVO>();
			for (TrackRecordDO trackRecordDO : trackRecordDOList) {
				trackRecordVOList.add(this.assembleD2V(trackRecordDO, null));
			}
		}
		page.setResult(trackRecordVOList);
		return page;
	}

	/**
	 * 方法描述: 事件跟踪新增方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param nameStr：查询条件班级名称
	 * @return 返回当前页的查询结果 Edit author: Edit on: 修改原因：
	 */
	public TrackRecordVO addQuestionair(Long csEventID,
			TrackRecordVO trackRecordVO) {
		this.checkParameterIsNull(csEventID,
				"custservice:custservice_parameter_null_exception");
		if (BlankUtil.isBlank(trackRecordVO)) {
			return null;
		}
		checkAddQuestionairIsTtue(csEventID);
		trackRecordVO.setCustomerServiceEventID(csEventID);
		TrackRecordDO trackRecordDO = trackRecordDao.saveObject(this
				.assembleV2D(trackRecordVO, null));
		trackRecordVO.setKeyword(trackRecordDO.getKeyword());
		return trackRecordVO;
	}

	/**
	 * 方法描述: 事件跟踪新增合法性验证方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param csEventID
	 * @return void Edit author: Edit on: 修改原因：
	 */
	public void checkAddQuestionairIsTtue(Long csEventID) {
		this.checkParameterIsNull(csEventID,
				"custservice:custservice_parameter_null_exception");
		CustServiceEventDO custServiceEventDO = custServiceEventDao
				.findById(csEventID);
		if (CustServiceEventDO.EVENT_STATUS_CANCEL_CODE
				.equals(custServiceEventDO.getEventStatuz())) {
			throw new BusinessException(
					"custservice:custservice_trackrecord_add_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: 事件跟踪作废方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param trackRecordVO
	 * @return TrackRecordVO
	 * @roseuid 4928189503B9
	 */
	public TrackRecordVO setAside(Long csEventID, TrackRecordVO trackRecordVO) {
		this.checkParameterIsNull(trackRecordVO.getKeyword(),
				"custservice:custservice_parameter_null_exception");
		if (BlankUtil.isBlank(trackRecordVO)) {
			return null;
		}
		TrackRecordDO trackRecordDO = this.checkSetAsideIsTtue(csEventID,
				trackRecordVO.getKeyword());
		trackRecordDO.setRemark(trackRecordVO.getRemark());
		trackRecordDO.setValidityFalse();
		modifyInfo(trackRecordDO.getSysInfo(), trackRecordVO.getModifier());
		trackRecordDao.updateObject(trackRecordDO);
		return trackRecordVO;
	}

	/**
	 * 方法描述: 事件跟踪作废合法性验证方法
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param csEventID,trackRecordId
	 * @return TrackRecordDO Edit author: Edit on: 修改原因：
	 */
	public TrackRecordDO checkSetAsideIsTtue(Long csEventId, Long trackRecordId) {
		this.checkParameterIsNull(csEventId,
				"custservice:custservice_parameter_null_exception");
		this.checkParameterIsNull(trackRecordId,
				"custservice:custservice_parameter_null_exception");
		CustServiceEventDO custServiceEventDO = custServiceEventDao
				.findById(csEventId);
		if (!CustServiceEventDO.EVENT_STATUS_NEW_CODE.equals(custServiceEventDO
				.getEventStatuz())
				&& !CustServiceEventDO.EVENT_STATUS_SUBMIT_CODE
						.equals(custServiceEventDO.getEventStatuz())) {
			throw new BusinessException(
					"custservice:custservice_trackrecord_setAside_exception",
					BusinessException.WARNING);
		}
		TrackRecordDO trackRecordDO = trackRecordDao.findById(trackRecordId);
		if (SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(trackRecordDO
				.getValidity())) {
			throw new BusinessException(
					"custservice:custservice_trackrecord_hassetAside_exception",
					BusinessException.WARNING);
		}
		return trackRecordDO;
	}

	/**
	 * 方法描述: AbstractQuestionaireVO to TrackRecordDO
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param questionaireVO,trackRecordDO
	 * @return TrackRecordDO Edit author: Edit on: 修改原因：
	 */
	private TrackRecordDO assembleV2D(TrackRecordVO trackRecordVO,
			TrackRecordDO trackRecordDO) {
		if (BlankUtil.isBlank(trackRecordVO)) {
			return null;
		}
		if (BlankUtil.isBlank(trackRecordDO)) {
			trackRecordDO = new TrackRecordDO();
		}
		BeanUtil.convertVoAsDo(trackRecordVO, trackRecordDO);
		if (!BlankUtil.isBlank(trackRecordVO.getCustomerServiceEventID())) {
			trackRecordDO.setCustomerServiceEventDO(new CustServiceEventDO(
					trackRecordVO.getCustomerServiceEventID()));
		}
		if (!BlankUtil.isBlank(trackRecordVO.getReturnVisitWayID())) {
			trackRecordDO.setReturnVisitWayDO(new ReturnVisitWayDO(
					trackRecordVO.getReturnVisitWayID()));
		}
		if (!BlankUtil.isBlank(trackRecordVO.getHouseID())) {
			trackRecordDO.setHouse(new HouseDomain(trackRecordVO.getHouseID()));
		}

		if (!BlankUtil.isBlank(trackRecordVO.getCreator())) {
			trackRecordDO.setSysInfo(this
					.createInfo(trackRecordVO.getCreator()));
		}

		return trackRecordDO;
	}

	/**
	 * 方法描述: TrackRecordDO to TrackRecordVO
	 *
	 * @author 胡煦 Create on:2008-12-24
	 * @param trackRecordDO,trackRecordVO
	 * @return TrackRecordVO Edit author: Edit on: 修改原因：
	 */
	private TrackRecordVO assembleD2V(TrackRecordDO trackRecordDO,
			TrackRecordVO trackRecordVO) {
		if (BlankUtil.isBlank(trackRecordDO)) {
			return null;
		}
		if (BlankUtil.isBlank(trackRecordVO)) {
			trackRecordVO = new TrackRecordVO();
		}
		PopulateUtil.populate(trackRecordDO, trackRecordVO);
		trackRecordVO.setFullAddress(trackRecordDO.getCustomerServiceEventDO() == null ? null
				: trackRecordDO.getCustomerServiceEventDO()
				.getFullAddress());
		trackRecordVO
				.setEventNo(trackRecordDO.getCustomerServiceEventDO() == null ? null
						: trackRecordDO.getCustomerServiceEventDO()
								.getEventNo());
		trackRecordVO.setHouseID(trackRecordDO.getHouse()==null?null:trackRecordDO.getHouse().getKeyword());
		trackRecordVO
				.setReturnVisitWayID(trackRecordDO.getReturnVisitWayDO() == null ? null
						: trackRecordDO.getReturnVisitWayDO().getKeyword());
		return trackRecordVO;
	}

	private ITrackRecordDao trackRecordDao;

	private ICustServiceEventDao custServiceEventDao;

	/**
	 * @param trackRecordDao
	 *            the trackRecordDao to set
	 */
	public void setTrackRecordDao(ITrackRecordDao trackRecordDao) {
		this.trackRecordDao = trackRecordDao;
	}

	/**
	 * @param custServiceEventDao
	 *            the custServiceEventDao to set
	 */
	public void setCustServiceEventDao(ICustServiceEventDao custServiceEventDao) {
		this.custServiceEventDao = custServiceEventDao;
	}
}
